package com.github.catvod.utils;

import android.util.Base64;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AgeUtil {

    private static final String KEY_BASE64 = "bmkgcG8gamllIG5pICoqIA=="; // Base64编码的密钥
    private static final String ALGORITHM = "AES";
    private static final String TRANSFORMATION = "AES/CBC/PKCS5Padding";
    public static String generateUUID() {
        final String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        char[] uuidChars = new char[36];
        Random random = new Random();

        // UUID格式中的固定位置字符
        uuidChars[8] = uuidChars[13] = uuidChars[18] = uuidChars[23] = '-';
        uuidChars[14] = '4';

        for (int i = 0; i < 36; i++) {
            if (uuidChars[i] == 0) { // 未初始化的位置
                int tmp = random.nextInt(0x10); // 生成0到15的随机数
                int randomCharIndex = i == 19 ? (tmp & 0x3) | 0x8 : tmp & 0xf; // 特殊处理第19位
                uuidChars[i] = chars.charAt(randomCharIndex);
            }
        }

        return new String(uuidChars);
    }

    public static List<String> extractIds(String input, int limit) {
        List<String> ids = new ArrayList<>();
        String regex = "id=['\"](\\w+)['\"]";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);

        while (matcher.find() && ids.size() < limit) {
            ids.add(matcher.group(1)); // 提取第一个捕获组的内容
        }

        return ids;
    }
    public static String hxm_encrypt(String data) throws Exception {
        byte[] keyBytes = Base64.decode(KEY_BASE64, Base64.DEFAULT);
        SecretKeySpec keySpec = new SecretKeySpec(keyBytes, ALGORITHM);
        IvParameterSpec ivSpec = new IvParameterSpec(keyBytes);

        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);

        byte[] encryptedBytes = cipher.doFinal(data.getBytes("UTF-8"));
        return bytesToHex(encryptedBytes);
    }

    public static String hxm_decrypt(String data) throws Exception {
        byte[] keyBytes = Base64.decode(KEY_BASE64, Base64.DEFAULT);
        SecretKeySpec keySpec = new SecretKeySpec(keyBytes, ALGORITHM);
        IvParameterSpec ivSpec = new IvParameterSpec(keyBytes);

        byte[] hexBytes = hexStringToByteArray(data);
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);

        byte[] decryptedBytes = cipher.doFinal(hexBytes);
        return new String(decryptedBytes, "UTF-8");
    }

    private static String bytesToHex(byte[] bytes) {
        StringBuilder hexString = new StringBuilder();
        for (byte b : bytes) {
            String hex = Integer.toHexString(0xff & b);
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex);
        }
        return hexString.toString();
    }

    private static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                    + Character.digit(s.charAt(i + 1), 16));
        }
        return data;
    }
}
